package cn.sh.ideal.web.view.display.host.monitor.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import cn.sh.ideal.web.view.display.host.monitor.entity.SearchServerInfoEntity;

@Controller
@RequestMapping(value = "/get")
@PropertySource(value = { "classpath:jdbc.properties" })
public class HostMonitorDataController {
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	private RestTemplate rs = new RestTemplate();

	@Value("${server_address}")
	private String serverAddress;

	/**
	 * 获取原系统主机信息
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/getServiceInfo", method = RequestMethod.POST)
	@ResponseBody
	public String getServiceInfo(@RequestBody SearchServerInfoEntity paramter) {

		String systemName = paramter.getSystemName();
		String belongAppliedSystem = paramter.getBelongAppliedSystem();

		String url = serverAddress+"/selectServerInfo?systemName="
				+ systemName + "&belongAppliedSystem=" + belongAppliedSystem;

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);

		String reult = exchange.getBody();
		return reult;
	}
	
	/**
	 * GrafanaUrl
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/getGrafanaUrl", method = RequestMethod.POST)
	@ResponseBody
	public String getGrafanaUrl(@RequestBody Integer id) {

		String url = serverAddress+"/getGrafanaUrl?id=" + id ;

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);

		String reult = exchange.getBody();
		return reult;
	}
	
	/**
	 * 获取Tree数据
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/gethostMonitorTreeData", method = RequestMethod.POST)
	@ResponseBody
	public String gethostMonitorTreeData() {
		
		String url = serverAddress+"/selecthostMonitorTreeData";

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);

		String reult = exchange.getBody();
		return reult;
	}
	
	/**
	 * 获取Tree数据
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/asyncGetHostMonitorTreeData", method = RequestMethod.POST)
	@ResponseBody
	public String asyncGetHostMonitorTreeData(@RequestBody SearchServerInfoEntity parameter) {
		String commentId = parameter.getCommentId();
		String url = serverAddress+"/asyncSelectHostMonitorTreeData?&commentId="+commentId;

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);

		String reult = exchange.getBody();
		return reult;
	}
}
